package de.revdev.llt.bean.managed;

import de.revdev.llt.bean.entities.Participants;
import de.revdev.llt.bean.entities.PublicClassRoom;
import de.revdev.llt.bean.entities.User;
import de.revdev.llt.bean.facade.ParticipantsFacade;
import de.revdev.llt.bean.facade.PublicClassRoomFacade;
import de.revdev.llt.bean.facade.UserFacade;
import de.revdev.llt.bean.utility.MessageProvider;
import java.io.Serializable;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.PostConstruct;
import javax.ejb.EJBException;
import javax.enterprise.context.RequestScoped;
import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.event.ActionEvent;
import javax.inject.Inject;
import javax.inject.Named;
import org.primefaces.event.SelectEvent;
import org.primefaces.event.UnselectEvent;

/**
 *
 * @author f.lindermaier
 */
@Named(value = "editParticipants")
@RequestScoped
public class EditParticipants implements Serializable {

    private ShowClassRoomBean showClassRoomBean;
    private UserFacade uf;
    private ParticipantsFacade pf;
    private PublicClassRoomFacade pcrf;
    private MessageProvider provider;
    private Participants selectedParticipant;
    private Participants newParticipants;
    private String email;
    private boolean showEdit;
    private UIComponent emailFiled;
    private User invitedUser;
    private static final Logger log = Logger.getLogger(EditParticipants.class.getName());

    /**
     * Creates a new instance of EditParticipants
     */
    public EditParticipants() {
    }

    @PostConstruct
    private void init() {
        newParticipants = new Participants();
        email = "";
        showEdit = false;
    }

    public boolean checkEmail() {
        try {
            PublicClassRoom pcr = (PublicClassRoom) showClassRoomBean.getClassRoom();
            invitedUser = uf.findByEmail(email);
            if (invitedUser.equals(pcr.getOwner())) {
                provider.addMessage(emailFiled.getClientId(), FacesMessage.SEVERITY_INFO, "editparticipantsBean_is_owner");
                return false;
            } else {
                for (Participants p : pcr.getParticipants()) {
                    if (invitedUser.equals(p.getUser())) {
                        provider.addMessage(emailFiled.getClientId(), FacesMessage.SEVERITY_INFO, "editparticipantsBean_is_participant");
                        return false;
                    }
                }
            }
            return true;
        } catch (EJBException nre) {
            provider.addMessage(emailFiled.getClientId(), FacesMessage.SEVERITY_ERROR, "editparticipantsBean_email_not_exist");
            log.log(Level.INFO, "EMailAddress {0} not listed", email);
            return false;
        }
    }

    public void invite(ActionEvent event) {
        if (checkEmail()) {
            PublicClassRoom pcr = (PublicClassRoom) showClassRoomBean.getClassRoom();
            newParticipants.setUser(invitedUser);
            newParticipants.setPublicClassRoom(pcr);
            pf.create(newParticipants);
            invitedUser.getParticipants().add(newParticipants);
            uf.edit(invitedUser);
            pcr.getParticipants().add(newParticipants);
            pcrf.edit(pcr);
            showClassRoomBean.refreshClassRoom();
            log.log(Level.INFO, "Invitation {0} sent to User {1} for PublicClassRoom {2}", new Object[]{newParticipants.getId(), invitedUser.getId(), pcr.getId()});
            selectedParticipant = null;
            newParticipants = new Participants();
            email = "";
            showEdit = false;
        } else {
            newParticipants = new Participants();
            showEdit = false;
        }
    }

    public void save(ActionEvent event) {
        pf.edit(selectedParticipant);
        showClassRoomBean.refreshClassRoom();
        log.log(Level.INFO, "Participation {0} edited", new Object[]{selectedParticipant.getId()});
        selectedParticipant = null;
        newParticipants = new Participants();
        email = "";
        showEdit = false;
    }

    public void cancel(ActionEvent event) {
        selectedParticipant = null;
        newParticipants = new Participants();
        email = "";
        showEdit = false;
    }

    public void onRowSelect(SelectEvent event) {
        selectedParticipant = (Participants) event.getObject();
        newParticipants = new Participants();
        showEdit = true;
    }

    public void onRowUnselect(UnselectEvent event) {
        selectedParticipant = new Participants();
        newParticipants = new Participants();
        showEdit = false;
    }

    @Inject
    public void setUserFacade(UserFacade userFacade) {
        this.uf = userFacade;
    }

    @Inject
    public void setParticipantsFacade(ParticipantsFacade participantsFacade) {
        this.pf = participantsFacade;
    }

    @Inject
    public void setPublicClassRoomFacade(PublicClassRoomFacade publicClassRoomFacade) {
        this.pcrf = publicClassRoomFacade;
    }

    @Inject
    public void setMessageProvider(MessageProvider messageProvider) {
        this.provider = messageProvider;
    }

    @Inject
    public void setShowClassRoomBean(ShowClassRoomBean showClassRoomBean) {
        this.showClassRoomBean = showClassRoomBean;
    }

    public Participants getSelectedParticipant() {
        return selectedParticipant;
    }

    public void setSelectedParticipant(Participants selectedParticipant) {
        this.selectedParticipant = selectedParticipant;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public Participants getNewParticipants() {
        return newParticipants;
    }

    public void setNewParticipants(Participants newParticipants) {
        this.newParticipants = newParticipants;
    }

    public boolean isShowEdit() {
        return showEdit;
    }

    public void setShowEdit(boolean showEdit) {
        this.showEdit = showEdit;
    }

    public UIComponent getEmailFiled() {
        return emailFiled;
    }

    public void setEmailFiled(UIComponent emailFiled) {
        this.emailFiled = emailFiled;
    }
}
